//
//  DiseaseViewController.m
//  WE
//
//  Created by apple on 15/11/21.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "DiseaseViewController.h"
#import "Size.h"
#import "ToolUtil.h"
#import "DoctorProficientCell.h"
#import "DrugViewController.h"
#import "TeamViewController.h"
#import "MoreAdviViewController.h"
#import "RelatedAdviViewController.h"

@interface DiseaseViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UILabel *detailLabel;
    BOOL flag;
    UITableView *tableV;
    UIView *headerView;
    NSArray *arr;
    NSInteger i;
    UIImageView *downImage;
    UILabel *titleLabel;
}


@end

@implementation DiseaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *downview = [[UIView alloc] initWithFrame:CGRectMake(0, WindowHeight - 49, WindowWidth, 49)];
    NSArray *Tarr = @[@"专家团队",@"相关医生",@"相关咨询",@"相关药品"];
    NSArray *iArr = @[@"ico-3",@"ico-2",@"ico2",@"ico1"];
    
    for (int k = 0; k < 4; k++) {
        UIButton *tButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tButton.frame = CGRectMake(k *WindowWidth/4, 0, WindowWidth/4, 49);
        tButton.backgroundColor = [UIColor whiteColor];
        UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(0, 29, WindowWidth/4, 20)];
        titleL.text = Tarr[k];
        titleL.textColor = [UIColor blackColor];
        titleL.textAlignment = NSTextAlignmentCenter;
        titleL.font = FONT(12);
        [tButton addSubview:titleL];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((WindowWidth/4 - 20)/2, 5, 20, 20)];
        imageView.image = [UIImage imageNamed:iArr[k]];
        [tButton addSubview:imageView];
        [ToolUtil createLine:0 superView:tButton lift:0 width:WindowWidth/4 hight:1 color:GRAYCOLOR];
        tButton.tag = 1200+k;
        [tButton addTarget:self action:@selector(presstButton:) forControlEvents:UIControlEventTouchUpInside];
        [downview addSubview:tButton];
    }
    [self.view addSubview:downview];
    // Do any additional setup after loading the view.
     arr = @[@"发病机制",@"临床表现",@"并发症",@"实验室检查",@"其他辅助检查",@"诊断",@"鉴别诊断",@"治疗",@"预后",@"预防"];
    
    headerView = [[UIView alloc] init];
    self.navigationItem.title = self.string;
    self.tabBarController.tabBar.hidden = YES;
    tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WindowWidth, WindowHeight - 49 - 64)];
    tableV.showsVerticalScrollIndicator = NO;
    [self.view addSubview:tableV];
    flag = YES;
    i = 0;
    
    tableV.delegate = self;
    tableV.dataSource = self;
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 100*Height)];
    imageV.image = [UIImage imageNamed:@"shouye5"];
    [headerView addSubview:imageV];
    UIView *blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 70*Height, WindowWidth, 30*Height)];
    blackView.backgroundColor = [UIColor blackColor];
    blackView.alpha = 0.8;
    [imageV addSubview:blackView];
    CGFloat wid = [ToolUtil autoWidth:self.string width:0 height:30*Height num:17];
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20*Width, 0, wid, 30*Height)];
    nameLabel.text = self.string;
    nameLabel.font = FONT(17);
    nameLabel.textColor = [UIColor whiteColor];
    [blackView addSubview:nameLabel];
    
    CGFloat subjectWid = [ToolUtil autoWidth:@"什么什么科" width:0 height:30*Height num:14];
    UILabel *subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(30*Width +wid, 0, subjectWid, 30*Height)];
    subjectLabel.font = FONT(14);
    subjectLabel.text = @"什么什么科";
    
    subjectLabel.textColor = [UIColor whiteColor];
    [blackView addSubview:subjectLabel];
    
    detailLabel = [[UILabel alloc] init];
    detailLabel.frame = CGRectMake(10, 110*Height, WindowWidth-20, 120*Height);
    detailLabel.font = FONT(14);
    detailLabel.text = @"牙周炎主要是由局部因素引起的牙周支持组织的慢性炎症。发病年龄以35岁以后较为多见。如龈炎未能及时治疗，炎症可由牙龈向深层扩散到牙周膜、牙槽骨和牙骨质而发展为牙周炎。由于早期多无明显自觉症状而易被忽视，待有症状时已较严重，甚至已不能保留牙齿。因而必须加强宣教，使患者早期就诊和及时治疗。 当机体抵抗力降低、牙周袋渗液引流不畅时，可形成牙周脓肿，是牙周炎发展到晚期，出现深牙周袋的一个常见的伴发症状。此时牙龈呈卵圆形突起，发红肿胀，表面光亮；牙齿松动度增加，有叩痛；患者伴有局部剧烈跳痛。同时，患者可有体温升高、全身不适，颌下淋巴结肿大、压痛等症状。";
    detailLabel.numberOfLines = 5;
    detailLabel.backgroundColor = [UIColor whiteColor];
    detailLabel.textColor = [UIColor blackColor];
    [headerView addSubview:detailLabel];
    UIButton *shrinkButton = [UIButton buttonWithType:UIButtonTypeCustom];
    shrinkButton.frame = CGRectMake(0, 240*Height, WindowWidth, 40*Height);
    CGFloat titleWid = [ToolUtil autoWidth:@"点击查看全部内容" width:0 height:30*Height num:14];
    [shrinkButton addTarget:self action:@selector(pressshrinkButton:) forControlEvents:UIControlEventTouchUpInside];
     titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((WindowWidth - titleWid)/2, 0, titleWid, 30*Height)];
    titleLabel.text = @"点击查看全部内容";
    titleLabel.font = FONT(14);
    
    titleLabel.textColor = [UIColor blackColor];
    [shrinkButton addSubview:titleLabel];
    
    [ToolUtil createLine:0 superView:shrinkButton lift:0 width:WindowWidth hight:1*Height color:[UIColor colorWithRed:0.7492 green:0.7492 blue:0.7492 alpha:1.0]];
    
    downImage = [[UIImageView alloc] initWithFrame:CGRectMake((WindowWidth - titleWid)/2 + titleWid + 5, 10*Height, 15*Width, 10*Height)];
    downImage.image = [UIImage imageNamed:@"arrow_bottom"];
    
    [shrinkButton addSubview:downImage];
    [ToolUtil createLine:31*Height superView:shrinkButton lift:0 width:WindowWidth hight:9*Height color:GRAYCOLOR];
    [headerView addSubview:shrinkButton];
    tableV.tableHeaderView = headerView;
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    
}

- (void)presstButton:(UIButton*)but{
    
    
    
    if (but.tag == 1200) {
        
        [self. navigationController pushViewController:[[TeamViewController alloc]init] animated:YES];
        
    }else if(but.tag == 1201){
        [self.navigationController pushViewController:[[MoreAdviViewController alloc] init] animated:YES];
        
    }else if (but.tag == 1202){
        [self.navigationController pushViewController:[[RelatedAdviViewController alloc] init] animated:YES];
        
    }else if (but.tag == 1203){
        [self.navigationController pushViewController:[[DrugViewController alloc] init] animated:YES];
    }
    
}
- (void)pressshrinkButton:(UIButton*)btu{
    
    if (detailLabel.numberOfLines == 5) {
        detailLabel.numberOfLines = 0;
        CGFloat heig = [ToolUtil customAutoHeigh:detailLabel.text width:WindowWidth num:14];
        
        //detailLabel.text = @"牙周炎主要是由局部因素引起的牙周支持组织的慢性炎症。发病年龄以35岁以后较为多见。如龈炎未能及时治疗，炎症可由牙龈向深层扩散到牙周膜、牙槽骨和牙骨质而发展为牙周炎。由于早期多无明显自觉症状而易被忽视，待有症状时已较严重，甚至已不能保留牙齿。因而必须加强宣教，使患者早期就诊和及时治疗。 当机体抵抗力降低、牙周袋渗液引流不畅时，可形成牙周脓肿，是牙周炎发展到晚期，出现深牙周袋的一个常见的伴发症状。此时牙龈呈卵圆形突起，发红肿胀，表面光亮；牙齿松动度增加，有叩痛；患者伴有局部剧烈跳痛。同时，患者可有体温升高、全身不适，颌下淋巴结肿大、压痛等症状。";
        detailLabel.frame = CGRectMake(10, 110*Height, WindowWidth-20, heig);
        btu.frame = CGRectMake(0, 120*Height+heig, WindowWidth, 30*Height);
        downImage.image = [UIImage imageNamed:@"arrow_up"];
        CGFloat titleWid = [ToolUtil autoWidth:@"收起" width:0 height:30*Height num:14];
        titleLabel.frame = CGRectMake((WindowWidth - titleWid)/2, 0, titleWid, 30*Height);
        titleLabel.text = @"收起";
        downImage.frame = CGRectMake((WindowWidth - titleWid)/2 + titleWid + 5, 10*Height, 15*Width, 10*Height);
    }else{
        detailLabel.numberOfLines = 5;
        detailLabel.frame = CGRectMake(10, 110*Height, WindowWidth-20, 120*Height);
        btu.frame = CGRectMake(0, 240*Height, WindowWidth, 30*Height);
        downImage.image = [UIImage imageNamed:@"arrow_bottom"];
        titleLabel.text = @"点击查看更多内容";
        CGFloat titleWid = [ToolUtil autoWidth:@"点击查看更多内容" width:0 height:30*Height num:14];
        titleLabel.frame = CGRectMake((WindowWidth - titleWid)/2, 0, titleWid, 30*Height);
        downImage.frame = CGRectMake((WindowWidth - titleWid)/2 + titleWid + 5, 10*Height, 15*Width, 10*Height);
    }
    
    [tableV reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return arr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if (section == i && !flag) {
        return 1;
    }
    
    return 0;
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
        static NSString *cellString = @"DoctorProficientCell";
        DoctorProficientCell *cell = [tableView dequeueReusableCellWithIdentifier:cellString];
        if (!cell) {
            cell = [[DoctorProficientCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellString];
        }
        
    
    cell.backgroundColor = GRAYCOLOR;
    cell.titleLbl.textColor = BLACKCOLOR;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [DoctorProficientCell cellHeight];
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section!=0) {
        
    
    
    
    UIView *headerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 50*Height)];
    headerV.backgroundColor = [UIColor whiteColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, WindowWidth, 50*Height);
    [button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = 800+section;
    [headerV addSubview:button];
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20*Height, 0, 300*Width, 49*Height)];
   
    label.text = arr[section];
    label.backgroundColor = [UIColor whiteColor];
    label.textColor = [UIColor blackColor];
    [button addSubview:label];
    UIImageView *imagev = [[UIImageView alloc] initWithFrame:CGRectMake(WindowWidth - 30*Width, 20*Height, 15*Width, 10*Height)];
    imagev.image = [UIImage imageNamed:@"arrow_bottom"];
    [button addSubview:imagev];
        if (flag == NO && i == section) {
             imagev.image = [UIImage imageNamed:@"arrow_up"];
        }
    [ToolUtil createLine:49*Height superView:button lift:0 width:WindowWidth hight:1*Height color:GRAYCOLOR];
        
    if (section == arr.count - 1) {
    [ToolUtil createLine:51*Height superView:headerV lift:0 width:WindowWidth hight:8*Height color:GRAYCOLOR];
    }
    
    return headerV;
    }
    
    
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        
        
        
        
        return detailLabel.frame.origin.y +detailLabel.frame.size.height + 50*Height;
    }else if(section == arr.count - 1){
        return 45*Height;
    }
    return 50*Height;
}

- (void)pressButton:(UIButton *)but{
    flag = !flag;
    
    
    i = but.tag - 800;
    [tableV reloadData];
    
    
   // [tableV reloadSections:[NSIndexSet indexSetWithIndex:but.tag - 800] withRowAnimation:UITableViewRowAnimationAutomatic];
    
   // NSIndexSet *set = [NSIndexSet indexSetWithIndex:but.tag - 800];
    //[tableV reloadSections:set withRowAnimation:UITableViewRowAnimationAutomatic];
    //int j = (int)but.tag - 800;
    
    
    
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
